package bb.lanxing.fragment.base;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.model.LatLng;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.view.View;
import android.view.ViewGroup;

import androidx.core.content.res.ResourcesCompat;
import androidx.fragment.app.FragmentActivity;

import bb.lanxing.R;
import bb.lanxing.lib.kt.model.geo.IGeoPoint;
import bb.lanxing.manager.BaiduLocationManager;
import bb.lanxing.manager.PlaceCouponSharedPreference;
import bb.lanxing.model.bean.Place;
import bb.lanxing.model.database.MapPOI;
import bb.lanxing.model.database.RouteBook;
import bb.lanxing.model.map.GeoBounds;
import bb.lanxing.model.map.GeoPoint;
import bb.lanxing.util.MapUtil;

public abstract class BaseMapFragment extends BaseFragment {
    public static final int OVERLAY_TYPE_NONE = 0;
    public static final int OVERLAY_TYPE_ROAD_BOOK = 1;
    public static final int OVERLAY_TYPE_NAV = 2;
    public static final int OVERLAY_TYPE_WORK_OUT = 3;
    public static final int OVERLAY_TYPE_ALTITUDE = 4;
    public static final int OVERLAY_TYPE_DISTANCE = 5;
    public static final int OVERLAY_TYPE_PACE = 7;
    public static final int OVERLAY_TYPE_SEGMENT = 8;
    public static final int OVERLAY_TYPE_POI = 9;
    public static final int OVERLAY_TYPE_SLOPE_LEVEL = 10;

    protected int compassMargin;
    protected Handler handler;
    protected InitListener initListener;
    public boolean isHideMap;
    protected Handler mapHandler;
    private HandlerThread mapThread;
    protected final Object syncLock = new Object();
    private boolean loadPoi = false;
    private boolean initialized = false;
    private Map<MapPOI, Object> customPoi = new HashMap();
    protected int locationMode = 1;
    protected ArrayList<ViewGroup> mInterceptTouchView = new ArrayList<>(2);

    protected BaiduLocationManager.LocationChangedListener listener;

    private final Runnable loadPoiRunnable = () -> {
        final List<MapPOI> queryPoi;
        GeoBounds screenRect = BaseMapFragment.this.getScreenRect();
        if (screenRect == null) {
            return;
        }
        float maxOrMinZoom = BaseMapFragment.this.getMaxOrMinZoom(true);
        float zoomLevel = BaseMapFragment.this.getZoomLevel();
        if (zoomLevel >= maxOrMinZoom - 5.0f) {
            queryPoi = MapPOI.queryPoi(screenRect, BaseMapFragment.this.getVisiblePOILevel(zoomLevel), 500);
        } else {
            queryPoi = MapPOI.queryPoi(screenRect, BaseMapFragment.this.getVisiblePOILevel(zoomLevel), 100);
        }
        BaseMapFragment.this.handler.post(new Runnable() {
            @Override
            public void run() {
                BaseMapFragment.this.drawPoi(queryPoi);
            }
        });
    };

    public interface InitListener {
        void onViewReady(BaseMapFragment fragment);
    }

    public interface MapViewListener<Map, P, M, PL> {
        void onDragMap(Map mapView, boolean end);

        void onMakerClick(M Marker);

        void onMapClick(Map mapView, P point);

        void onMapLongClick(Map mapView, P point);

        void onPolylineClick(PL polyline);

        void onZoom(float zoom);
    }

    protected abstract void adjustCompassPosition(int compassMargin);

    public abstract void changeMapType(int type);

    public abstract boolean clearOverlay(int type);

    public abstract void displayBound(GeoBounds geoBounds);

    public abstract Object drawLine(int type, List<IGeoPoint> points, int color, boolean bound);

    public abstract Object drawLine1(int type, List<LatLng> points, int color, boolean bound);

    public abstract void drawLushu(RouteBook routeBook, int color, boolean bound);

    public abstract void drawLushu(RouteBook routeBook, int color, boolean bound, Runnable runnable);

    public abstract Object drawMarker(int type, LatLng location, Drawable icon, String title, float anchorX, float anchorY);

    public abstract Object drawMarker(int type, IGeoPoint location, Drawable icon, String title, float anchorX, float anchorY);

    public abstract Object drawMarker(int type, IGeoPoint location, Drawable icon, String title, float anchorX, float anchorY, int zIndex);

    public abstract LatLng getCenter();

    protected Handler.Callback getHandlerCallback() {
        return null;
    }

    public abstract float getMaxOrMinZoom(boolean max);

    public abstract List<Object> getOverlay(int type);

    protected abstract int getPoiZIndex(int type);

    public abstract Object getProjection();

    protected abstract GeoBounds getScreenRect();

    protected abstract int getVisiblePOILevel(float zoom);

    public abstract float getZoomLevel();

    protected abstract boolean isPOISelected(MapPOI mapPOI);

    public abstract void moveTo(double lat, double lng);

    public abstract void removeOverlay(Object overlay, int type);

    public abstract <Map, P, M, PL> void setMapViewListener(MapViewListener<Map, P, M, PL> mapViewListener);

    protected abstract void setSelectedPOIMarker(Object marker);

    public abstract void zoomIn();

    public abstract void zoomOut();

    private void closeMapThread() {
        HandlerThread handlerThread = this.mapThread;
        if (handlerThread == null || !handlerThread.isAlive()) {
            return;
        }
        this.mapThread.quit();
        this.mapThread = null;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.handler = new Handler(Looper.getMainLooper(), getHandlerCallback());
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        this.initialized = true;
        InitListener initListener = this.initListener;
        if (initListener != null) {
            initListener.onViewReady(this);
        }
        flushPoi();
    }

    @Override
    public void onDestroy() {
        Handler handler = this.mapHandler;
        if (handler != null) {
            handler.removeCallbacks(this.loadPoiRunnable);
        }
        closeMapThread();
        super.onDestroy();
    }

    public Object drawMarker(int type, LatLng location, View view, float anchorX, float anchorY) {
        if (view == null) {
            return null;
        }
        return drawMarker(type, location, MapUtil.convertView(view), (String) null, anchorX, anchorY);
    }

    public void setLocationMode(int locationMode) {
        this.locationMode = locationMode;
    }

    public int getLocationMode() {
        return this.locationMode;
    }

    public void hideMap(Context context, boolean isHideMap) {
        this.isHideMap = isHideMap;
    }

    public void setInitListener(InitListener initListener) {
        this.initListener = initListener;
        if (initListener == null || !this.initialized) {
            return;
        }
        initListener.onViewReady(this);
    }

    protected synchronized void drawPoi(List<MapPOI> pois) {
        if (pois == null) {
            return;
        }
        LinkedList<MapPOI> linkedList = new LinkedList(pois);
        LinkedList<MapPOI> linkedList2 = new LinkedList();
        for (MapPOI mapPOI : this.customPoi.keySet()) {
            if (linkedList.contains(mapPOI)) {
                linkedList.remove(mapPOI);
            } else {
                linkedList2.add(mapPOI);
            }
        }
        for (MapPOI mapPOI2 : linkedList2) {
            removeOverlay(this.customPoi.remove(mapPOI2), 9);
        }
        float[] fArr = new float[2];
        for (MapPOI mapPOI3 : linkedList) {
            GeoPoint fromEarth = GeoPoint.fromEarth(mapPOI3.getLatitude(), mapPOI3.getLongitude());
            boolean isPOISelected = isPOISelected(mapPOI3);
            Object drawMarker = drawMarker(BaseMapFragment.OVERLAY_TYPE_POI, fromEarth, getPoiDrawable(mapPOI3, fArr, isPOISelected), "", fArr[0], fArr[1], getPoiZIndex(mapPOI3.getType()));
            this.customPoi.put(mapPOI3, drawMarker);
            if (isPOISelected) {
                setSelectedPOIMarker(drawMarker);
            }
            if (drawMarker instanceof Marker) {
                Bundle bundle = new Bundle();
                bundle.putParcelable("map_poi", mapPOI3);
                ((Marker) drawMarker).setExtraInfo(bundle);
            } else if (drawMarker instanceof org.osmdroid.views.overlay.Marker) {
                ((org.osmdroid.views.overlay.Marker) drawMarker).setRelatedObject(mapPOI3);
            }
        }
    }

    public Drawable getPoiDrawable(MapPOI poi, float[] anchor, boolean selected) {
        int type = poi.getType();
        int i = R.drawable.ic_place_blue;
        if (type == 1) {
            anchor[0] = 0.5f;
            anchor[1] = 0.95f;
        } else if (type == 2) {
            Place place = (Place) poi.getExtObject();
            i = (place.getPlaceDiscountList().isEmpty() || !PlaceCouponSharedPreference.getInstance().isShowNewPlaceCoupon((long) place.getServerId())) ? selected ? R.drawable.ic_marker_shop_auth_selected : R.drawable.ic_marker_shop_auth : selected ? R.drawable.ic_marker_shop_auth_coupon_selected : R.drawable.ic_marker_shop_auth_coupon;
            anchor[0] = 0.5f;
            anchor[1] = selected ? 0.82f : 0.5f;
        } else {
            anchor[0] = 0.5f;
            anchor[1] = 0.95f;
        }
        FragmentActivity activity = getActivity();
        if (activity == null) {
            return null;
        }
        return ResourcesCompat.getDrawable(activity.getResources(), i, activity.getTheme());
    }

    public void flushPoi() {
        if (this.loadPoi) {
            this.mapHandler.removeCallbacks(this.loadPoiRunnable);
            this.mapHandler.postDelayed(this.loadPoiRunnable, 500L);
        }
    }

    public void post(Runnable runnable) {
        this.handler.post(runnable);
    }

    public void postDelayed(Runnable runnable, long millis) {
        this.handler.postDelayed(runnable, millis);
    }

    public int getCompassMargin() {
        return this.compassMargin;
    }

    public void setLocationChangedListener(BaiduLocationManager.LocationChangedListener locationChangedListener) {
        listener = locationChangedListener;
    }
}